/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-11-22 16:19:19
 * @LastEditors: huangtianyang
 * @LastEditTime: 2024-05-09 16:28:14
 * @FilePath: \cxl-driving-training\src\views\mini\issue\index.ts
 */
import { BaseData, BaseMethod } from '@/utils/basc-data'
import { tableConfig } from './config'
import { reactive } from 'vue'
import { ElMessage, FormInstance } from 'element-plus'
import { issueAdd, issueDisable, issueEdit, issueList } from '@/apis/issue'

export default class IssueData extends BaseData implements BaseMethod {
    dialog = reactive({
        visible: false,
        title: '新增问题',
        width: '800px',
        type: 'create'
    })
    dialogForm = reactive({
        title: '',
        content: '',
        id: ''
    })
    rules = {
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        content: [{ required: true, message: '请输入内容', trigger: 'blur' }]
    }
    constructor() {
        super(tableConfig)
        this.getData()
    }
    getData = (arg?: any) => {
        issueList(this.pageData.pagination.pageNo, this.pageData.pagination.pageSize).then(res => {
            if (Number(res.data.code) === 200) {
                this.pageData.table.data.length = 0
                this.pageData.pagination.total = res.data.result.total
                const arr: any[] = res.data.result.records
                arr.forEach((element: any) => {
                    element.status = Number(element.disable) === 0 ? true : false
                    this.pageData.table.data.push(element)
                })
            }
        })
    }
    onSearch = (arg?: any) => {
        console.log(arg)
        this.getData()
    }
    onReset = (arg?: any) => {
        this.getData()
    }
    onPaginationChange = (arg?: any) => {
        this.pageData.pagination.pageNo = arg
        this.getData()
    }
    onPaginationSize = (arg?: any) => {
        this.pageData.pagination.pageNo = 1
        this.pageData.pagination.pageSize = arg
        this.getData()
    }
    onOperation = (arg?: any) => {
        const { type, data } = arg
        this.dialog.type = type
        console.log(type, data)
        if (type === 'create') {
            this.dialog.visible = true
            this.dialog.title = '新增问题'
            this.dialogForm.id = ''
            this.dialogForm.content = ''
            this.dialogForm.title = ''
        } else if (type === 'edit') {
            this.dialog.visible = true
            this.dialog.title = '编辑问题'
            this.dialogForm.id = data.id
            this.dialogForm.content = data.content
            this.dialogForm.title = data.title
        } else if (type === 'switch') {
            issueDisable(data.status ? 0 : 1, data.id)
        }
    }
    //监听弹窗关闭
    onCloseDialog = (formEl: FormInstance | undefined) => {
        if (!formEl) return
        formEl.resetFields()
        this.dialog.visible = false
    }
    //监听弹窗点击确定按钮
    submitForm = async (formEl: FormInstance | undefined) => {
        if (!formEl) return
        await formEl.validate((valid, fields) => {
            if (valid) {
                console.log('submit!', this.dialogForm)
                if (this.dialog.type === 'create') {
                    issueAdd({ title: this.dialogForm.title, content: this.dialogForm.content }).then(res => {
                        if (Number(res.data.code) === 200) {
                            this.dialog.visible = false
                            this.getData()
                            ElMessage.success('新增成功')
                        }
                    })
                } else {
                    issueEdit({
                        title: this.dialogForm.title,
                        content: this.dialogForm.content,
                        id: this.dialogForm.id
                    }).then(res => {
                        if (Number(res.data.code) === 200) {
                            this.dialog.visible = false
                            this.getData()
                            ElMessage.success('修改成功')
                        }
                    })
                }
            } else {
                console.log('error submit!', fields)
            }
        })
    }
}
